package example;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService()
public class TaxCalculator {
  @WebMethod
  // 计算个人所得税的函数
  public double calculateMonthlyTax(double monthlyIncome) {
    // 定义税率和速算扣除数（以人民币为单位）
    final double[] THRESHOLDS = {3000, 12000, 25000, 35000, 55000, 80000};
    // 此处的阈值是按月计算的，因为函数输入的是月收入
    final double[] MONTHLY_TAX_RATES = {0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45};
    double tax = 0;
    double taxableIncome = monthlyIncome - 5000; // 假设起征点为5000元

    if (taxableIncome <= THRESHOLDS[0]) {
      tax = taxableIncome * MONTHLY_TAX_RATES[0];
    } else if (taxableIncome <= THRESHOLDS[1]) {
      tax = (taxableIncome - THRESHOLDS[0]) * MONTHLY_TAX_RATES[1] + THRESHOLDS[0] * MONTHLY_TAX_RATES[0];
    } else if (taxableIncome <= THRESHOLDS[2]) {
      tax = (taxableIncome - THRESHOLDS[1]) * MONTHLY_TAX_RATES[2] + (THRESHOLDS[1] - THRESHOLDS[0]) * MONTHLY_TAX_RATES[1] + THRESHOLDS[0] * MONTHLY_TAX_RATES[0];
    } else if (taxableIncome <= THRESHOLDS[3]) {
      tax = (taxableIncome - THRESHOLDS[2]) * MONTHLY_TAX_RATES[3] + (THRESHOLDS[2] - THRESHOLDS[1]) * MONTHLY_TAX_RATES[2] + (THRESHOLDS[1] - THRESHOLDS[0]) * MONTHLY_TAX_RATES[1] + THRESHOLDS[0] * MONTHLY_TAX_RATES[0];
    } else if (taxableIncome <= THRESHOLDS[4]) {
      tax = (taxableIncome - THRESHOLDS[3]) * MONTHLY_TAX_RATES[4] + (THRESHOLDS[3] - THRESHOLDS[2]) * MONTHLY_TAX_RATES[3] + (THRESHOLDS[2] - THRESHOLDS[1]) * MONTHLY_TAX_RATES[2] + (THRESHOLDS[1] - THRESHOLDS[0]) * MONTHLY_TAX_RATES[1] + THRESHOLDS[0] * MONTHLY_TAX_RATES[0];
    } else if (taxableIncome <= THRESHOLDS[5]) {
      tax = (taxableIncome - THRESHOLDS[4]) * MONTHLY_TAX_RATES[5] + (THRESHOLDS[4] - THRESHOLDS[3]) * MONTHLY_TAX_RATES[4] + (THRESHOLDS[3] - THRESHOLDS[2]) * MONTHLY_TAX_RATES[3] + (THRESHOLDS[2] - THRESHOLDS[1]) * MONTHLY_TAX_RATES[2] + (THRESHOLDS[1] - THRESHOLDS[0]) * MONTHLY_TAX_RATES[1] + THRESHOLDS[0] * MONTHLY_TAX_RATES[0];
    } else {
      tax = (taxableIncome - THRESHOLDS[5]) * MONTHLY_TAX_RATES[6] + (THRESHOLDS[5] - THRESHOLDS[4]) * MONTHLY_TAX_RATES[5] + (THRESHOLDS[4] - THRESHOLDS[3]) * MONTHLY_TAX_RATES[4] + (THRESHOLDS[3] - THRESHOLDS[2]) * MONTHLY_TAX_RATES[3] + (THRESHOLDS[2] - THRESHOLDS[1]) * MONTHLY_TAX_RATES[2] + (THRESHOLDS[1] - THRESHOLDS[0]) * MONTHLY_TAX_RATES[1] + THRESHOLDS[0] * MONTHLY_TAX_RATES[0];
    }

    // 由于计算过程中可能存在极小的负数误差（例如浮点数运算），确保最终结果不为负
    return Math.max(0, tax);
  }
  public static void main(String[] argv) {
    Object implementor = new TaxCalculator();
    String address = "http://localhost:9000/tax_calculator";
    Endpoint.publish(address, implementor);
  }
}
